package com.mac.cxcybackground.controller;

import com.mac.cxcybackground.bean.Users;
import com.mac.cxcybackground.mapper.UsersMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

@Controller
public class LoginController {
    @GetMapping({"/","/login"})
    public String login(){
        return "login";
    }
    /**
     * 解决表单重复提交   重定向方法
     * @return
     */
    @Autowired
    UsersMapper usersMapper;
    @PostMapping("/login")
    public String index(HttpSession session, Model model, Users users){
      Map<String,Object> map =new HashMap<>();
      map.put("userName",users.getUserName());
      map.put("password",users.getPassword());
        if(usersMapper.selectByMap(map).size()!=0){
            session.setAttribute("loginUser",users);
            return "redirect:/index.html";
        }else{
            model.addAttribute("msg","用户名或密码错误");
            //回到登录页
            return "login";
        }

    }

    @GetMapping("/index.html")
    public String IndexPage(){

        return "index";
    }
}
